﻿#region Using Directives

using System;
using System.Web;
using System.Web.UI.WebControls;

using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;

using SharePointStu.ItemAuditing.Controls;
using SharePointStu.SharePoint.General.Helpers;
using SharePointStu.SharePoint.General.Logging;

#endregion Using Directives

namespace SharePointStu.ItemAuditing.Pages
{
    public partial class ViewListAuditLog : Base.BaseAuditLog
    {
        #region Private Members

        const string QUERYSTRING_LISTID = "ListId";

        SPList _list;

        #endregion Private Members

        #region Public Properties

        /// <summary>
        /// Get the identifier for the <see cref="T:Microsoft.SharePoint.SPList"/>  object to retrieve the audit information for.
        /// </summary>
        public Guid ListId
        {
            get
            {
                if (Common.IsGuid(Page.Request[QUERYSTRING_LISTID]))
                {
                    return new Guid(Page.Request[QUERYSTRING_LISTID]);
                }
                return Guid.Empty;
            }
        }

        #endregion Public Properties

        #region Initialisation

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            if (this.ListId == Guid.Empty)
            {
                Logger.LogErrorLocal(EventId.Error, CategoryId.Auditing, ErrorLevel.High,
                                     string.Format("Exception while loading {0} - Invalid List ID.", this.GetType().Namespace), null);
            }
            else
            {
                _list = SPContext.Current.Web.Lists.GetList(this.ListId, false);
                lblListName.Text = _list.Title;
                lblListURL.Text = _list.RootFolder.ServerRelativeUrl;
            }
        }

        #endregion Initialisation

        #region Overridden Methods

        /// <summary> 
        /// Overrides the breadcrumbs with a new one in the format [site]>[list]>[page].
        /// </summary> 
        protected override SiteMapNode CustomSiteMapResolve(object sender, SiteMapResolveEventArgs e)
        {
            if (!IsSamePage(Context, e.Context))
            {
                e.Provider.EnableLocalization = true;
                return e.Provider.CurrentNode;
            }

            SiteMapNode pageTitleNode;
            try
            {
                // Turn off localisation, so that any custom nodes do not get overwritten.
                e.Provider.EnableLocalization = false;

                // Clone some node to get a valid node object and modify it. 
                SiteMapNode listNode = e.Provider.RootNode.ChildNodes[0].Clone();
                listNode.Url = _list.DefaultViewUrl;
                listNode.Title = _list.Title;
                listNode.ChildNodes = new SiteMapNodeCollection();

                // Page node
                pageTitleNode = new SiteMapNode(e.Provider, Guid.NewGuid().ToString());
                pageTitleNode.Title = string.IsNullOrEmpty(this.Page.Title) ? DEFAULT_PAGE_TITLE : this.Page.Title;
                listNode.ChildNodes.Add(pageTitleNode);
                pageTitleNode.ParentNode = listNode;
            }
            catch
            {
                return e.Provider.CurrentNode;
            }
            return pageTitleNode;
        }

        protected override SPBasePermissions RightsRequired
        {
            get
            {
                SPBasePermissions permissions = base.RightsRequired | SPBasePermissions.EditListItems;
                return permissions;
            }
        }

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);

            this.AuditLogViewer.WebId = this.WebId;
            this.AuditLogViewer.ListId = this.ListId;
            this.AuditLogViewer.AuditType = AuditLogView.AuditTypes.List;
        }

        #endregion Overridden Methods
    }
}
